package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractBackCostDAO;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.ContractDetailDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.SaleCostDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.BackCost;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.ContractDetail;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.ManageReport;
import com.feeyin.radar.crm.model.SaleCost;
import com.feeyin.radar.crm.model.SalePeriodReport;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.model.SmallTotalSalePeriodReport;
import com.feeyin.radar.crm.model.TotalSalePeriodReport;
import com.feeyin.radar.crm.service.ManageReportService;
import com.feeyin.radar.sys.dao.BusinessTargetDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Department;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;
import com.feeyin.radar.sys.service.DepartmentService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManageReportServiceImpl
  implements ManageReportService
{
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private BusinessTargetDAO businessTargetDAO;
  private ContractBackCostDAO contractBackCostDAO;
  private ContractDAO contractDAO;
  private SaleCostDAO saleCostDAO;
  private CustomerDAO customerDAO;
  private SaleRecordDAO saleRecordDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private DepartmentService departmentService;
  private LinkmanDAO linkmanDAO;
  private ContractDetailDAO contractDetailDAO;

  public ContractDetailDAO getContractDetailDAO()
  {
    return this.contractDetailDAO;
  }

  public void setContractDetailDAO(ContractDetailDAO contractDetailDAO) {
    this.contractDetailDAO = contractDetailDAO;
  }

  public DepartmentService getDepartmentService() {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public BusinessTargetDAO getBusinessTargetDAO() {
    return this.businessTargetDAO;
  }

  public void setBusinessTargetDAO(BusinessTargetDAO businessTargetDAO) {
    this.businessTargetDAO = businessTargetDAO;
  }

  public ContractBackCostDAO getContractBackCostDAO() {
    return this.contractBackCostDAO;
  }

  public void setContractBackCostDAO(ContractBackCostDAO contractBackCostDAO) {
    this.contractBackCostDAO = contractBackCostDAO;
  }

  public ContractDAO getContractDAO() {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public SaleCostDAO getSaleCostDAO() {
    return this.saleCostDAO;
  }

  public void setSaleCostDAO(SaleCostDAO saleCostDAO) {
    this.saleCostDAO = saleCostDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public Map getBusinessFinishStatus(String isViewDept, ManageReport manageReport)
  {
    if (manageReport == null) return null;
    if (manageReport.getStartDate() == null) return null;
    if (manageReport.getEndDate() == null) return null;

    Map map = new HashMap();

    List departments = new ArrayList();
    String[] deptIds = (String[])null;
    int sign = 0;
    if ((!StringUtils.isEmpty(isViewDept)) && (isViewDept.equals("1")))
    {
      if (StringUtils.isEmpty(manageReport.getDeptIds())) return null;
      deptIds = manageReport.getDeptIds().split(",");
      sign = 1;
    }
    else {
      if (manageReport.getUserId() == null) return null;
      Integer deptId = this.userDAO.getUserDeptIdById(manageReport.getUserId());
      if (deptId == null) return null;
      deptIds = new String[1];
      deptIds[0] = deptId.toString();
    }

    if ((deptIds == null) || (deptIds.length <= 0)) return null;
    BigDecimal totalBusinessTarget = new BigDecimal(0);
    BigDecimal totalFinishCost = new BigDecimal(0);
    BigDecimal totalFinishRate = new BigDecimal(0);
    BigDecimal totalBackCost = new BigDecimal(0);
    BigDecimal totalReceivable = new BigDecimal(0);
    BigDecimal totalSaleCost = new BigDecimal(0);
    BigDecimal totalPredictCost = new BigDecimal(0);
    BigDecimal totalContractCost = new BigDecimal(0);

    for (int i = 0; i < deptIds.length; i++) {
      if (!StringUtils.isEmpty(deptIds[i])) {
        Department department = new Department();
        department = this.departmentDAO.getDepartment(Integer.valueOf(Integer.parseInt(deptIds[i])));
        if (department != null) {
          BigDecimal deptBusinessTarget = new BigDecimal(0);
          BigDecimal deptFinishCost = new BigDecimal(0);
          BigDecimal deptFinishRate = new BigDecimal(0);
          BigDecimal deptBackCost = new BigDecimal(0);
          BigDecimal deptReceivable = new BigDecimal(0);
          BigDecimal deptSaleCost = new BigDecimal(0);
          BigDecimal deptPredictCost = new BigDecimal(0);
          BigDecimal deptContractCost = new BigDecimal(0);

          List users = new ArrayList();
          users = this.userDAO.getUserActivityByDeptId(department.getId());
          if ((users != null) && (users.size() > 0)) {
            for (int j = 0; j < users.size(); j++) {
              Users user = new Users();
              user = (Users)users.get(j);
              BigDecimal businessTarget = new BigDecimal(0);
              BigDecimal finishCost = new BigDecimal(0);
              BigDecimal finishRate = new BigDecimal(0);
              BigDecimal backCost = new BigDecimal(0);
              BigDecimal receivable = new BigDecimal(0);
              BigDecimal saleCost = new BigDecimal(0);
              BigDecimal predictCost = new BigDecimal(0);
              BigDecimal contractCost = new BigDecimal(0);

              businessTarget = this.businessTargetDAO.getPeriodTargetBySalePersonId(user.getId(), manageReport.getStartDate(), manageReport.getEndDate());
              if ((businessTarget != null) && (businessTarget.compareTo(new BigDecimal(0)) > 0)) {
                businessTarget = businessTarget.setScale(0, 4);
              }
              finishCost = this.businessTargetDAO.getPeriodFinishTargetBySalePersonId(user.getId(), manageReport.getStartDate(), manageReport.getEndDate());
              user.getSalePeriodReport().setBusinessTarget(businessTarget);
              user.getSalePeriodReport().setFinishCost(finishCost);
              if ((finishCost != null) && (finishCost.compareTo(new BigDecimal(0)) > 0) && 
                (businessTarget != null) && (businessTarget.compareTo(new BigDecimal(0)) > 0)) {
                finishRate = finishCost.divide(businessTarget, 12, 4);
                if ((finishRate != null) && (finishRate.compareTo(new BigDecimal(0)) > 0)) {
                  finishRate = finishRate.multiply(new BigDecimal(100));
                  user.getSalePeriodReport().setFinishRate(finishRate);
                }
              }

              BackCost bc = new BackCost();
              bc.setSalePersonId(user.getId());
              bc.setStartDate(manageReport.getStartDate());
              bc.setEndDate(manageReport.getEndDate());
              backCost = this.contractBackCostDAO.getTotalBackCostByDynamic(bc);
              user.getSalePeriodReport().setBackCost(backCost);

              Contract contract = new Contract();
              contract.setSalePersonId(user.getId());

              contract.setMoreStatus("2,4");
              receivable = this.contractDAO.getTotalContractReceivableByDynamic(contract);
              user.getSalePeriodReport().setReceivable(receivable);

              SaleCost sc = new SaleCost();
              sc.setApplyPersonId(user.getId());
              sc.setStartDate(manageReport.getStartDate());
              sc.setEndDate(manageReport.getEndDate());
              sc.setStatus("2");
              saleCost = this.saleCostDAO.getTotalSaleCostByDynamic(sc);
              user.getSalePeriodReport().setSaleCost(saleCost);

              Customer customer = new Customer();
              customer.setSalePersonId(user.getId());
              customer.setCategorys("1");
              customer.setIsSaleStage("1");
              customer.setStartDate(manageReport.getStartDate());
              customer.setEndDate(manageReport.getEndDate());
              predictCost = this.customerDAO.getTotalPredictCostByDynamic(customer);
              user.getSalePeriodReport().setPredictCost(predictCost);

              contract = new Contract();
              contract.setSalePersonId(user.getId());
              contract.setStartDate(manageReport.getStartDate());
              contract.setEndDate(manageReport.getEndDate());
              contract.setMoreStatus("2,4");
              contractCost = this.contractDAO.getTotalContractCostByDynamic(contract);
              user.getSalePeriodReport().setContractCost(contractCost);

              if ((sign == 0) && (user.getId().compareTo(manageReport.getUserId()) == 0)) {
                department.getUsers().add(user);
                deptBusinessTarget = businessTarget;
                deptFinishCost = finishCost;
                if ((deptBusinessTarget != null) && (deptBusinessTarget.compareTo(new BigDecimal(0)) > 0) && 
                  (deptFinishCost != null) && (deptFinishCost.compareTo(new BigDecimal(0)) > 0)) {
                  BigDecimal temp = new BigDecimal(0);
                  temp = deptFinishCost.divide(deptBusinessTarget, 12, 4);
                  if ((temp != null) && (temp.compareTo(new BigDecimal(0)) > 0)) {
                    deptFinishRate = temp.multiply(new BigDecimal(100));
                  }
                }

                deptBackCost = backCost;
                deptReceivable = receivable;
                deptSaleCost = saleCost;
                deptPredictCost = predictCost;
                deptContractCost = contractCost;
              } else if (sign == 1) {
                department.getUsers().add(user);
                if (businessTarget != null) {
                  deptBusinessTarget = deptBusinessTarget.add(businessTarget);
                }
                if (finishCost != null) {
                  deptFinishCost = deptFinishCost.add(finishCost);
                }
                if (finishRate != null) {
                  deptFinishRate = deptFinishRate.add(finishRate);
                }
                if (backCost != null) {
                  deptBackCost = deptBackCost.add(backCost);
                }
                if (receivable != null) {
                  deptReceivable = deptReceivable.add(receivable);
                }
                if (saleCost != null) {
                  deptSaleCost = deptSaleCost.add(saleCost);
                }
                if (predictCost != null) {
                  deptPredictCost = deptPredictCost.add(predictCost);
                }
                if (contractCost != null) {
                  deptContractCost = deptContractCost.add(contractCost);
                }
              }
            }

            department.getSmallTotalSalePeriodReport().setBusinessTarget(deptBusinessTarget);
            department.getSmallTotalSalePeriodReport().setFinishCost(deptFinishCost);
            if ((deptBusinessTarget != null) && (deptBusinessTarget.compareTo(new BigDecimal(0)) > 0) && 
              (deptFinishCost != null) && (deptFinishCost.compareTo(new BigDecimal(0)) > 0)) {
              BigDecimal temp = new BigDecimal(0);
              temp = deptFinishCost.divide(deptBusinessTarget, 12, 4);
              if ((temp != null) && (temp.compareTo(new BigDecimal(0)) > 0)) {
                deptFinishRate = temp.multiply(new BigDecimal(100));
              }
            }
            department.getSmallTotalSalePeriodReport().setFinishRate(deptFinishRate);
            department.getSmallTotalSalePeriodReport().setBackCost(deptBackCost);
            department.getSmallTotalSalePeriodReport().setReceivable(deptReceivable);
            department.getSmallTotalSalePeriodReport().setSaleCost(deptSaleCost);
            department.getSmallTotalSalePeriodReport().setPredictCost(deptPredictCost);
            department.getSmallTotalSalePeriodReport().setContractCost(deptContractCost);

            departments.add(department);
            if (deptBusinessTarget != null) {
              totalBusinessTarget = totalBusinessTarget.add(deptBusinessTarget);
            }
            if (deptFinishCost != null) {
              totalFinishCost = totalFinishCost.add(deptFinishCost);
            }
            if (deptFinishRate != null) {
              totalFinishRate = totalFinishRate.add(deptFinishRate);
            }
            if (deptBackCost != null) {
              totalBackCost = totalBackCost.add(deptBackCost);
            }
            if (deptReceivable != null) {
              totalReceivable = totalReceivable.add(deptReceivable);
            }
            if (deptSaleCost != null) {
              totalSaleCost = totalSaleCost.add(deptSaleCost);
            }
            if (deptPredictCost != null) {
              totalPredictCost = totalPredictCost.add(deptPredictCost);
            }
            if (deptContractCost != null) {
              totalContractCost = totalContractCost.add(deptContractCost);
            }
          }
        }
      }
    }
    TotalSalePeriodReport totalSalePeriodReport = new TotalSalePeriodReport();
    totalSalePeriodReport.setBusinessTarget(totalBusinessTarget);
    totalSalePeriodReport.setFinishCost(totalFinishCost);
    if ((totalBusinessTarget != null) && (totalBusinessTarget.compareTo(new BigDecimal(0)) > 0) && 
      (totalFinishCost != null) && (totalFinishCost.compareTo(new BigDecimal(0)) > 0)) {
      BigDecimal temp = new BigDecimal(0);
      temp = totalFinishCost.divide(totalBusinessTarget, 12, 4);
      if ((temp != null) && (temp.compareTo(new BigDecimal(0)) > 0)) {
        totalFinishRate = temp.multiply(new BigDecimal(100));
      }
    }
    totalSalePeriodReport.setFinishRate(totalFinishRate);
    totalSalePeriodReport.setBackCost(totalBackCost);
    totalSalePeriodReport.setReceivable(totalReceivable);
    totalSalePeriodReport.setSaleCost(totalSaleCost);
    totalSalePeriodReport.setPredictCost(totalPredictCost);
    totalSalePeriodReport.setContractCost(totalContractCost);

    map.put("departments", departments);
    map.put("totalSalePeriodReport", totalSalePeriodReport);
    return map;
  }

  public Map getPeriodNewSaleCustomer(String isViewDept, ManageReport manageReport)
  {
    if (manageReport.getStartDate() == null) return null;
    if (manageReport.getEndDate() == null) return null;

    Map map = new HashMap();

    List departments = new ArrayList();
    String[] deptIds = (String[])null;
    int sign = 0;
    if ((!StringUtils.isEmpty(isViewDept)) && (isViewDept.equals("1")))
    {
      if (StringUtils.isEmpty(manageReport.getDeptIds())) return null;
      deptIds = manageReport.getDeptIds().split(",");
      sign = 1;
    }
    else {
      if (manageReport.getUserId() == null) return null;
      Integer deptId = this.userDAO.getUserDeptIdById(manageReport.getUserId());
      if (deptId == null) return null;
      deptIds = new String[1];
      deptIds[0] = deptId.toString();
    }

    if ((deptIds == null) || (deptIds.length <= 0)) return null;
    Integer totalCustomerNum = Integer.valueOf(0);
    BigDecimal totalPredictCost = new BigDecimal(0);
    for (int i = 0; i < deptIds.length; i++) {
      if (!StringUtils.isEmpty(deptIds[i])) {
        Department department = new Department();
        department = this.departmentDAO.getDepartment(Integer.valueOf(Integer.parseInt(deptIds[i])));
        if (department != null) {
          Integer deptCustomerNum = Integer.valueOf(0);
          BigDecimal deptPredictCost = new BigDecimal(0);

          List users = new ArrayList();
          users = this.userDAO.getUserActivityByDeptId(department.getId());
          if ((users != null) && (users.size() > 0)) {
            for (int j = 0; j < users.size(); j++) {
              Users user = new Users();
              user = (Users)users.get(j);
              BigDecimal predictCost = new BigDecimal(0);

              List customers = new ArrayList();
              Customer customer = new Customer();
              customer.setCustomerCategory("1");
              customer.setSalePersonId(user.getId());
              customer.setIsSaleStage("1");
              customers = this.customerDAO.getCustomersByDynamic(customer);
              if ((customers != null) && (customers.size() > 0)) {
                for (int k = 0; k < customers.size(); k++) {
                  customer = new Customer();
                  customer = (Customer)customers.get(k);
                  Date minSaleRecordDate = this.saleRecordDAO.getSaleRecordMinDateByCustomerIdAndStatus(customer.getId(), "0");
                  if ((minSaleRecordDate == null) || (
                    ((minSaleRecordDate.compareTo(manageReport.getStartDate()) < 0) || 
                    (minSaleRecordDate.compareTo(manageReport.getEndDate()) > 0)) && (
                    (customer.getAssignDate() == null) || (customer.getCustomerSourceId().intValue() != 2) || 
                    (customer.getAssignDate().compareTo(manageReport.getStartDate()) < 0) || 
                    (customer.getAssignDate().compareTo(manageReport.getEndDate()) > 0)))) continue;
                  SaleRecord saleRecord = new SaleRecord();
                  saleRecord = this.saleRecordDAO.getSaleRecordLastLinkByCustomerIdStatus(customer.getId(), "0", manageReport.getStartDate(), manageReport.getEndDate());
                  if (saleRecord != null) {
                    customer.setSaleRecord(saleRecord);
                  }
                  if (customer.getPredictCost() != null) {
                    predictCost = predictCost.add(customer.getPredictCost());
                  }

                  if ((customer != null) && (customer.getSaleStageId() != null)) {
                    SaleStageSet saleStage = new SaleStageSet();
                    saleStage = this.saleStageSetDAO.getSaleStageByStageCode(customer.getSaleStageId().toString());
                    if (saleStage != null) {
                      customer.setSaleStage(saleStage.getStageName());
                    }
                  }
                  user.getCustomers().add(customer);
                }

                user.getSalePeriodReport().setPredictCost(predictCost);

                if ((user.getCustomers() != null) && (user.getCustomers().size() > 0)) {
                  if ((sign == 0) && (user.getId().compareTo(manageReport.getUserId()) == 0)) {
                    department.getUsers().add(user);
                    deptCustomerNum = Integer.valueOf(deptCustomerNum.intValue() + user.getCustomers().size());
                    deptPredictCost = predictCost;
                  } else if (sign == 1) {
                    department.getUsers().add(user);
                    deptCustomerNum = Integer.valueOf(deptCustomerNum.intValue() + user.getCustomers().size());
                    if (predictCost != null) {
                      deptPredictCost = deptPredictCost.add(predictCost);
                    }
                  }
                }
              }
            }

            if ((department.getUsers() != null) && (department.getUsers().size() > 0)) {
              department.getSmallTotalSalePeriodReport().setCustomerNum(deptCustomerNum);
              department.getSmallTotalSalePeriodReport().setPredictCost(deptPredictCost);
              departments.add(department);
            }
          }
          if (deptCustomerNum != null) {
            totalCustomerNum = Integer.valueOf(totalCustomerNum.intValue() + deptCustomerNum.intValue());
          }
          if (deptPredictCost != null) {
            totalPredictCost = totalPredictCost.add(deptPredictCost);
          }
        }
      }
    }

    TotalSalePeriodReport totalSalePeriodReport = new TotalSalePeriodReport();
    totalSalePeriodReport.setCustomerNum(totalCustomerNum);
    totalSalePeriodReport.setPredictCost(totalPredictCost);

    map.put("departments", departments);
    map.put("totalSalePeriodReport", totalSalePeriodReport);
    return map;
  }

  public List getConstractByCustomerForUser(Contract constract) {
    List ConstactCustomers = this.contractDAO.getContractByCustomer(constract);
    return ConstactCustomers;
  }

  public Integer getCountForConstractByCustomerForUser(Contract contract)
  {
    return this.contractDAO.getCountForContractByCustomer(contract);
  }

  public List getContractByUsers(Contract contract, String isViewDeptManageReport, String isViewAllManageReport, Integer userid, Page page) {
    contract.setPageStartRow(page.getPageStartRow());
    contract.setPageEndRow(page.getPageEndRow());
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      contract.setSalePersonIds(salePersonIds);

      return this.contractDAO.getContractByUsers(contract);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.contractDAO.getContractByUsers(contract);
    }
    contract.setSalePersonId(userid);
    return this.contractDAO.getContractByUsers(contract);
  }

  public Integer gerCountForContractByUsers(Contract contract, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      contract.setSalePersonIds(salePersonIds);
      return this.contractDAO.getCountForContractByUsers(contract);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.contractDAO.getCountForContractByUsers(contract);
    }
    contract.setSalePersonId(userid);
    return this.contractDAO.getCountForContractByUsers(contract);
  }

  public Map getBussinessFinishStatusForSale(String isViewDeptManageReport, String isViewAllManageReport, ManageReport manageReport)
  {
    Map model = new HashMap();
    List users = new ArrayList();
    BigDecimal totalbusinessTarget = new BigDecimal(0);
    BigDecimal totalfinishRate = new BigDecimal(0);
    BigDecimal totalfinishCost = new BigDecimal(0);
    BigDecimal totalcontractCost = new BigDecimal(0);
    BigDecimal totalbackCost = new BigDecimal(0);
    BigDecimal totalreceivable = new BigDecimal(0);
    BigDecimal totalsaleCost = new BigDecimal(0);
    BigDecimal totalgrossBackCost = new BigDecimal(0);
    BigDecimal totalContractGross = new BigDecimal(0);
    BigDecimal totalreceivableGross = new BigDecimal(0);
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(manageReport.getUserId());

      String[] personIds = salePersonIds.split(",");
      for (int i = 0; i < personIds.length; i++) {
        Users user = this.userDAO.getUser(Integer.valueOf(Integer.parseInt(personIds[i])));
        user = UsersalesInMonth(user, manageReport);
        totalbusinessTarget = user.getSalePeriodReport().getBusinessTarget().add(totalbusinessTarget);
        if (user.getSalePeriodReport().getContractCost() != null) {
          totalcontractCost = user.getSalePeriodReport().getContractCost().add(totalcontractCost);
        }
        if (user.getSalePeriodReport().getGrossBackCost() != null) {
          totalgrossBackCost = user.getSalePeriodReport().getGrossBackCost().add(totalgrossBackCost);
        }
        if (user.getSalePeriodReport().getReceivableGross() != null) {
          totalreceivableGross = user.getSalePeriodReport().getReceivableGross().add(totalreceivableGross);
        }
        if (user.getSalePeriodReport().getContractGross() != null) {
          totalContractGross = user.getSalePeriodReport().getContractGross().add(totalContractGross);
        }
        if (user.getSalePeriodReport().getFinishCost() != null) {
          totalfinishCost = user.getSalePeriodReport().getFinishCost().add(totalfinishCost);
        }
        if (user.getSalePeriodReport().getBackCost() != null) {
          totalbackCost = user.getSalePeriodReport().getBackCost().add(totalbackCost);
        }
        if (user.getSalePeriodReport().getReceivable() != null) {
          totalreceivable = user.getSalePeriodReport().getReceivable().add(totalreceivable);
        }
        if (user.getSalePeriodReport().getSaleCost() != null) {
          totalsaleCost = user.getSalePeriodReport().getSaleCost().add(totalsaleCost);
        }
        users.add(user);
      }

      if ((totalfinishCost != null) && (totalfinishCost.compareTo(new BigDecimal(0)) > 0) && 
        (totalbusinessTarget != null) && (totalbusinessTarget.compareTo(new BigDecimal(0)) > 0)) {
        totalfinishRate = totalfinishCost.divide(totalbusinessTarget, 12, 4);
        if ((totalfinishRate != null) && (totalfinishRate.compareTo(new BigDecimal(0)) > 0))
        {
          totalfinishRate = totalfinishRate.multiply(new BigDecimal(100));
        }
      }
      TotalSalePeriodReport totalSalePeriodReport = new TotalSalePeriodReport();
      totalSalePeriodReport.setFinishCost(totalfinishCost);
      totalSalePeriodReport.setGrossBackCost(totalgrossBackCost);
      totalSalePeriodReport.setBusinessTarget(totalbusinessTarget);
      totalSalePeriodReport.setFinishRate(totalfinishRate);
      totalSalePeriodReport.setContractCost(totalcontractCost);
      totalSalePeriodReport.setBackCost(totalbackCost);
      totalSalePeriodReport.setReceivable(totalreceivable);
      totalSalePeriodReport.setSaleCost(totalsaleCost);
      totalSalePeriodReport.setContractGross(totalContractGross);
      totalSalePeriodReport.setReceivableGross(totalreceivableGross);
      model.put("manageReport", manageReport);
      model.put("users", users);
      model.put("totalSalePeriodReport", totalSalePeriodReport);
      return model;
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      List userList = this.userDAO.getUsers();

      for (int j = 0; j < userList.size(); j++) {
        Users user = (Users)userList.get(j);
        user = UsersalesInMonth(user, manageReport);
        totalbusinessTarget = user.getSalePeriodReport().getBusinessTarget().add(totalbusinessTarget);
        if (user.getSalePeriodReport().getContractCost() != null) {
          totalcontractCost = user.getSalePeriodReport().getContractCost().add(totalcontractCost);
        }
        if (user.getSalePeriodReport().getGrossBackCost() != null) {
          totalgrossBackCost = user.getSalePeriodReport().getGrossBackCost().add(totalgrossBackCost);
        }
        if (user.getSalePeriodReport().getReceivableGross() != null) {
          totalreceivableGross = user.getSalePeriodReport().getReceivableGross().add(totalreceivableGross);
        }
        if (user.getSalePeriodReport().getContractGross() != null) {
          totalContractGross = user.getSalePeriodReport().getContractGross().add(totalContractGross);
        }
        if (user.getSalePeriodReport().getFinishCost() != null) {
          totalfinishCost = user.getSalePeriodReport().getFinishCost().add(totalfinishCost);
        }
        if (user.getSalePeriodReport().getBackCost() != null) {
          totalbackCost = user.getSalePeriodReport().getBackCost().add(totalbackCost);
        }
        if (user.getSalePeriodReport().getReceivable() != null) {
          totalreceivable = user.getSalePeriodReport().getReceivable().add(totalreceivable);
        }
        if (user.getSalePeriodReport().getSaleCost() != null) {
          totalsaleCost = user.getSalePeriodReport().getSaleCost().add(totalsaleCost);
        }
        users.add(user);
      }
      if ((totalfinishCost != null) && (totalfinishCost.compareTo(new BigDecimal(0)) > 0) && 
        (totalbusinessTarget != null) && (totalbusinessTarget.compareTo(new BigDecimal(0)) > 0)) {
        totalfinishRate = totalfinishCost.divide(totalbusinessTarget, 12, 4);
        if ((totalfinishRate != null) && (totalfinishRate.compareTo(new BigDecimal(0)) > 0))
        {
          totalfinishRate = totalfinishRate.multiply(new BigDecimal(100));
        }
      }
      TotalSalePeriodReport totalSalePeriodReport = new TotalSalePeriodReport();
      totalSalePeriodReport.setFinishCost(totalfinishCost);
      totalSalePeriodReport.setGrossBackCost(totalgrossBackCost);
      totalSalePeriodReport.setBusinessTarget(totalbusinessTarget);
      totalSalePeriodReport.setFinishRate(totalfinishRate);
      totalSalePeriodReport.setContractCost(totalcontractCost);
      totalSalePeriodReport.setBackCost(totalbackCost);
      totalSalePeriodReport.setReceivable(totalreceivable);
      totalSalePeriodReport.setSaleCost(totalsaleCost);
      totalSalePeriodReport.setContractGross(totalContractGross);
      totalSalePeriodReport.setReceivableGross(totalreceivableGross);
      model.put("users", users);
      model.put("manageReport", manageReport);
      model.put("totalSalePeriodReport", totalSalePeriodReport);
      return model;
    }
    Integer userid = manageReport.getUserId();
    Users user = this.userDAO.getUser(userid);
    user = UsersalesInMonth(user, manageReport);
    TotalSalePeriodReport totalSalePeriodReport = new TotalSalePeriodReport();
    totalSalePeriodReport.setFinishCost(totalfinishCost);
    totalSalePeriodReport.setGrossBackCost(totalgrossBackCost);
    totalSalePeriodReport.setBusinessTarget(user.getSalePeriodReport().getBusinessTarget());
    totalSalePeriodReport.setFinishRate(user.getSalePeriodReport().getFinishRate());
    totalSalePeriodReport.setContractCost(user.getSalePeriodReport().getContractCost());
    totalSalePeriodReport.setBackCost(user.getSalePeriodReport().getBackCost());
    totalSalePeriodReport.setReceivable(user.getSalePeriodReport().getReceivable());
    totalSalePeriodReport.setSaleCost(user.getSalePeriodReport().getSaleCost());
    totalSalePeriodReport.setContractGross(user.getSalePeriodReport().getContractGross());
    totalSalePeriodReport.setReceivableGross(user.getSalePeriodReport().getReceivableGross());
    users.add(user);
    model.put("users", users);
    model.put("manageReport", manageReport);
    model.put("totalSalePeriodReport", totalSalePeriodReport);
    return model;
  }

  public Users UsersalesInMonth(Users user, ManageReport manageReport) {
    BigDecimal businessTarget = new BigDecimal(0);
    BigDecimal finishRate = new BigDecimal(0);
    BigDecimal finishCost = new BigDecimal(0);
    BigDecimal contractCost = new BigDecimal(0);
    BigDecimal backCost = new BigDecimal(0);
    BigDecimal receivable = new BigDecimal(0);
    BigDecimal saleCost = new BigDecimal(0);
    BigDecimal grossBackCost = new BigDecimal(0);
    BigDecimal contractGross = new BigDecimal(0);
    BigDecimal receivableGross = new BigDecimal(0);

    BackCost bc = new BackCost();
    bc.setSalePersonId(user.getId());
    bc.setStartDate(manageReport.getStartDate());
    bc.setEndDate(manageReport.getEndDate());
    backCost = this.contractBackCostDAO.getTotalBackCostByDynamic(bc);
    user.getSalePeriodReport().setBackCost(backCost);

    BackCost bc1 = new BackCost();
    bc1.setSalePersonId(user.getId());
    bc1.setStartDate(manageReport.getStartDate());
    bc1.setEndDate(manageReport.getEndDate());
    bc1.setGrossBackCostId(Integer.valueOf(2));
    grossBackCost = this.contractBackCostDAO.getTotalBackCostByDynamic(bc1);
    user.getSalePeriodReport().setGrossBackCost(grossBackCost);

    Contract contract = new Contract();
    contract.setSalePersonId(user.getId());

    contract.setMoreStatus("2");
    receivable = this.contractDAO.getTotalContractReceivableByDynamic(contract);
    user.getSalePeriodReport().setReceivable(receivable);

    contract = new Contract();
    contract.setSalePersonId(user.getId());
    if ((this.contractDAO.getUsersAllContractGrossBackCost(contract) == null) && (this.contractDAO.getUsersAllContractDetailGrossCost(contract) != null))
      receivableGross = this.contractDAO.getUsersAllContractDetailGrossCost(contract);
    else if ((this.contractDAO.getUsersAllContractGrossBackCost(contract) != null) && (this.contractDAO.getUsersAllContractDetailGrossCost(contract) != null)) {
      receivableGross = this.contractDAO.getUsersAllContractDetailGrossCost(contract).subtract(this.contractDAO.getUsersAllContractGrossBackCost(contract));
    }
    user.getSalePeriodReport().setReceivableGross(receivableGross);

    contract = new Contract();
    contract.setSalePersonId(user.getId());
    contract.setStartDate(manageReport.getStartDate());
    contract.setEndDate(manageReport.getEndDate());
    contract.setMoreStatus("2,4");
    contractCost = this.contractDAO.getTotalContractCostByDynamic(contract);
    user.getSalePeriodReport().setContractCost(contractCost);

    ContractDetail cd = new ContractDetail();
    cd.setSalePersonId(user.getId());
    cd.setStartDate(manageReport.getStartDate());
    cd.setEndDate(manageReport.getEndDate());
    cd.setMoreStatus("2,4");
    contractGross = this.contractDetailDAO.getTotalContractGross(cd);
    user.getSalePeriodReport().setContractGross(contractGross);

    SaleCost sc = new SaleCost();
    sc.setApplyPersonId(user.getId());
    sc.setStartDate(manageReport.getStartDate());
    sc.setEndDate(manageReport.getEndDate());
    sc.setStatus("2");
    saleCost = this.saleCostDAO.getTotalSaleCostByDynamic(sc);
    user.getSalePeriodReport().setSaleCost(saleCost);

    businessTarget = this.businessTargetDAO.getPeriodTargetBySalePersonId(user.getId(), manageReport.getStartDate(), manageReport.getEndDate());
    if ((businessTarget != null) && (businessTarget.compareTo(new BigDecimal(0)) > 0)) {
      businessTarget = businessTarget.setScale(0, 4);
    }

    finishCost = this.businessTargetDAO.getPeriodFinishTargetBySalePersonId(user.getId(), manageReport.getStartDate(), manageReport.getEndDate());

    user.getSalePeriodReport().setBusinessTarget(businessTarget);
    user.getSalePeriodReport().setFinishCost(finishCost);
    if ((finishCost != null) && (finishCost.compareTo(new BigDecimal(0)) > 0) && 
      (businessTarget != null) && (businessTarget.compareTo(new BigDecimal(0)) > 0)) {
      finishRate = finishCost.divide(businessTarget, 12, 4);
      if ((finishRate != null) && (finishRate.compareTo(new BigDecimal(0)) > 0))
      {
        finishRate = finishRate.multiply(new BigDecimal(100));
        user.getSalePeriodReport().setFinishRate(finishRate);
      }
    }
    return user;
  }

  public List getContractInfoByUserId(Contract contract)
  {
    return this.contractDAO.getContractInfoByUserId(contract);
  }

  public Map getCustomerContractInfo(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid, Page page)
  {
    Map map = new HashMap();
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      customer.setPageStartRow(page.getPageStartRow());
      customer.setPageEndRow(page.getPageEndRow());
      map.put("report", this.customerDAO.getOldCustomerContractInfo(customer));
      map.put("Industry", this.customerDAO.getCustomerCountByIndustry(customer));
      map.put("Area", this.customerDAO.getCustomerCountByArea(customer));
      map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyType(customer));
      return map;
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      customer.setPageStartRow(page.getPageStartRow());
      customer.setPageEndRow(page.getPageEndRow());
      map.put("report", this.customerDAO.getOldCustomerContractInfo(customer));
      map.put("Industry", this.customerDAO.getCustomerCountByIndustry(customer));
      map.put("Area", this.customerDAO.getCustomerCountByArea(customer));
      map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyType(customer));
      return map;
    }
    customer.setSalePersonId(userid);
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    map.put("report", this.customerDAO.getOldCustomerContractInfo(customer));
    map.put("Industry", this.customerDAO.getCustomerCountByIndustry(customer));
    map.put("Area", this.customerDAO.getCustomerCountByArea(customer));
    map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyType(customer));
    return map;
  }

  public Map getAllCustomerInfoForContract(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid, Page page)
  {
    Map map = new HashMap();
    Integer oldCustomerCount = getCountOldCustomerContractInfo(customer, isViewDeptManageReport, isViewAllManageReport, userid);
    Integer newCustomerCount = Integer.valueOf(Integer.valueOf(getCountAllCustomerInfoForContract(customer, isViewDeptManageReport, isViewAllManageReport, userid).intValue()).intValue() - oldCustomerCount.intValue());
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      customer.setPageStartRow(page.getPageStartRow());
      customer.setPageEndRow(page.getPageEndRow());
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      map.put("report", this.customerDAO.getAllCustomerInfoForContract(customer));
      map.put("Industry", this.customerDAO.getCustomerCountByIndustryByAll(customer));
      map.put("Area", this.customerDAO.getCustomerCountByAreaByAll(customer));
      map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyTypeByAll(customer));
      map.put("oldCustomerCount", oldCustomerCount);
      map.put("newCustomerCount", newCustomerCount);
      return map;
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      customer.setPageStartRow(page.getPageStartRow());
      customer.setPageEndRow(page.getPageEndRow());
      map.put("report", this.customerDAO.getAllCustomerInfoForContract(customer));
      map.put("Industry", this.customerDAO.getCustomerCountByIndustryByAll(customer));
      map.put("Area", this.customerDAO.getCustomerCountByAreaByAll(customer));
      map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyTypeByAll(customer));
      map.put("oldCustomerCount", oldCustomerCount);
      map.put("newCustomerCount", newCustomerCount);
      return map;
    }
    customer.setSalePersonId(userid);
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    map.put("report", this.customerDAO.getAllCustomerInfoForContract(customer));
    map.put("Industry", this.customerDAO.getCustomerCountByIndustryByAll(customer));
    map.put("Area", this.customerDAO.getCustomerCountByAreaByAll(customer));
    map.put("CompanyType", this.customerDAO.getCustomerCountByCompanyTypeByAll(customer));
    map.put("oldCustomerCount", oldCustomerCount);
    map.put("newCustomerCount", newCustomerCount);
    return map;
  }

  public Integer getCountAllCustomerInfoForContract(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      return this.customerDAO.getCountAllCustomerInfoForContract(customer);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.customerDAO.getCountAllCustomerInfoForContract(customer);
    }
    customer.setSalePersonId(userid);
    return this.customerDAO.getCountAllCustomerInfoForContract(customer);
  }

  public Integer getCountOldCustomerContractInfo(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      return this.customerDAO.getCountOldCustomerContractInfo(customer);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.customerDAO.getCountOldCustomerContractInfo(customer);
    }
    customer.setSalePersonId(userid);
    return this.customerDAO.getCountOldCustomerContractInfo(customer);
  }

  public List getLinkManMailInfo(Linkman linkman, String isViewDeptManageReport, String isViewAllManageReport, Integer userid, Page page)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      linkman.setSalePersons(salePersonIds);
      linkman.setPageStartRow(page.getPageStartRow());
      linkman.setPageEndRow(page.getPageEndRow());
      return this.linkmanDAO.getLinkManMailInfo(linkman);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      linkman.setPageStartRow(page.getPageStartRow());
      linkman.setPageEndRow(page.getPageEndRow());
      return this.linkmanDAO.getLinkManMailInfo(linkman);
    }
    linkman.setSalePersonId(userid);
    linkman.setPageStartRow(page.getPageStartRow());
    linkman.setPageEndRow(page.getPageEndRow());
    return this.linkmanDAO.getLinkManMailInfo(linkman);
  }

  public Integer getCountLinkManMailInfo(Linkman linkman, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      linkman.setSalePersons(salePersonIds);
      return this.linkmanDAO.getCountLinkManMailInfo(linkman);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.linkmanDAO.getCountLinkManMailInfo(linkman);
    }
    linkman.setSalePersonId(userid);
    return this.linkmanDAO.getCountLinkManMailInfo(linkman);
  }

  public List getEveryMonthNewCustomerCount(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      return this.customerDAO.getEveryMonthNewCustomerCount(customer);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.customerDAO.getEveryMonthNewCustomerCount(customer);
    }
    customer.setSalePersonId(userid);
    return this.customerDAO.getEveryMonthNewCustomerCount(customer);
  }

  public List getTotalYearNewCustomerCount(Customer customer, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      customer.setSalePerson(salePersonIds);
      return this.customerDAO.getTotalYearNewCustomerCount(customer);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.customerDAO.getTotalYearNewCustomerCount(customer);
    }
    customer.setSalePersonId(userid);
    return this.customerDAO.getTotalYearNewCustomerCount(customer);
  }

  public List getEveryMonthNewContractCount(Contract contract, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      contract.setSalePersonIds(salePersonIds);
      return this.contractDAO.getEveryMonthNewContractCount(contract);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.contractDAO.getEveryMonthNewContractCount(contract);
    }
    contract.setSalePersonId(userid);
    return this.contractDAO.getEveryMonthNewContractCount(contract);
  }

  public List getTotalYearNewContractCount(Contract contract, String isViewDeptManageReport, String isViewAllManageReport, Integer userid)
  {
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
      contract.setSalePersonIds(salePersonIds);
      return this.contractDAO.getTotalYearNewContractCount(contract);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      return this.contractDAO.getTotalYearNewContractCount(contract);
    }
    contract.setSalePersonId(userid);
    return this.contractDAO.getTotalYearNewContractCount(contract);
  }

  public List getBackCostContractByDynamic(BackCost backCost)
  {
    List list = this.contractBackCostDAO.getBackCostContractByDynamic(backCost);
    BackCost bc = new BackCost();
    bc.setSalePersonId(backCost.getSalePersonId());
    bc.setStartDate(backCost.getStartDate());
    bc.setEndDate(backCost.getEndDate());
    bc.setGrossBackCostId(Integer.valueOf(2));
    for (int i = 0; i < list.size(); i++) {
      BackCost backcost1 = (BackCost)list.get(i);
      bc.setContractId(backcost1.getContractId());
      bc.setBusinessStatus("1");
      backcost1.setYeJitotalcost(this.contractBackCostDAO.getYeJiBackCostContractByDynamic(bc));
    }
    return list;
  }
  public List getSaleStages() {
    return this.saleStageSetDAO.getSaleStages();
  }
  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }
}